package com.example.b_308.software.view.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.b_308.software.R;
import com.example.b_308.software.controller.user.UserController;
import com.example.b_308.software.model.Staff;
import com.example.b_308.software.service.IUserView;
import com.example.b_308.software.view.base.BaseActivity;
import com.example.b_308.software.view.main.MainActivity;

/**
 * Created by B-308 on 2017/11/23.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener,IUserView {
    EditText et_phone, et_password;
    ImageView iv_phone_delete,iv_password_delete;
    Button bt_login;
    TextView tv_forget_password,tv_register;
    UserController userController;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         initView();
        initData();
        initListener();
    }

    private void initListener() {
       iv_phone_delete.setOnClickListener(this);
        iv_password_delete.setOnClickListener(this);
        bt_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
       tv_forget_password.setOnClickListener (this);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s.length()==0){
            iv_phone_delete.setVisibility(View.GONE);
        }else{
            iv_phone_delete.setVisibility(View.VISIBLE);

        }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
       et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()==0){
                    iv_password_delete.setVisibility(View.GONE);
                }else{
                   iv_password_delete.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }




    private void initData() {
        userController=new UserController(this);
    }

    private void initView() {
        getTitleBar().setVisibility(View.GONE);
        setContentView(R.layout.activity_login);
        et_phone= (EditText) findViewById(R.id.et_phone);
        et_password= (EditText) findViewById(R.id.et_password);
        iv_phone_delete= (ImageView) findViewById(R.id.iv_phone_delete);
        iv_password_delete= (ImageView) findViewById(R.id.iv_password_delete);
       bt_login= (Button) findViewById(R.id.but_login);
        tv_register= (TextView) findViewById(R.id.tv_register);
       tv_forget_password= (TextView) findViewById(R.id.tv_forget_password);

    }
    public  void onClick(View v){
        switch (v.getId()){
            case R.id.iv_phone_delete:
                et_phone.setText("");
                break;
            case R.id.iv_password_delete:
                et_password.setText("");
                break;
            case R.id.but_login:
                userController.login(et_phone.getText().toString(),et_password.getText().toString());
                break;
            case R.id.tv_register:
                tend2(RegisterActivity.class);
                break;
            case R.id.tv_forget_password:
               tent2(ForgetpwActivity.class);
                break;



        }
    }
    private void tend2(Class<RegisterActivity> forgetpwActivityClass) {
    }

    @Override
    public void loginSuccess(Staff staff) {
        tent2(MainActivity.class);
    }

    @Override
    public void registerSuccess(Staff staff) {

    }

    @Override
    public void getCodeSuccess() {

    }
}



